package com.haofengsoft.scce.adapter.trade;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TradeAdapter extends BaseAdapter {

	Context context;
	List<TradeModel> tradeModelList;

	public TradeAdapter(Context context, List<TradeModel> tradeModelList) {
		this.context = context;
		this.tradeModelList = tradeModelList;
	}

	@Override
	public int getCount() {
		return this.tradeModelList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.tradeModelList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void addTradeModelList(List<TradeModel> tradeModelList) {
		this.tradeModelList.addAll(tradeModelList);
	}

	@Override
	public View getView(int position, View converView, ViewGroup parent) {

		View view = null;
		if (converView == null) {
			view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.main_trade_item, parent, false);
		} else {
			view = (ViewGroup) converView;
		}
		TextView tradeCommodityNameTv = (TextView) view.findViewById(R.id.trade_item_commodity_name_tv);
		TextView tradeCommodityPriceTv = (TextView) view.findViewById(R.id.trade_item_commodity_price_tv);
		ImageView tradeCommodityIv = (ImageView) view.findViewById(R.id.trade_item_pic_iv);
		TextView tradeStatusTv = (TextView) view.findViewById(R.id.trade_item_commodity_status_tv);
		TextView tradeFinishTimeTv = (TextView) view.findViewById(R.id.trade_item_finish_time_tv);
		TextView tradeSellerNameTv = (TextView) view.findViewById(R.id.trade_item_seller_tv);
		TradeModel tradeModel = (TradeModel) getItem(position);

		tradeCommodityNameTv.setText(tradeModel.getTradeCommodityName());
		tradeCommodityPriceTv.setText(tradeModel.getTradeCommodityPrice());
		tradeFinishTimeTv.setText(tradeModel.getTradeFinishTime());
		tradeSellerNameTv.setText(tradeModel.getTradeSellerName());
		switch (tradeModel.getTradeStatus()) {
		case 1:
			tradeStatusTv.setText("竞价中");
			break;
		default:
			break;
		}
		ImageLoader.getInstance().displayImage(
				StringUtil.isBlank(tradeModel.getTradeImgUrl()) ? "" : ConstantConfig.API_URL
						+ tradeModel.getTradeImgUrl(), tradeCommodityIv);

		return view;
	}
}
